﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpiderHelper.Tools.MMOnly
{
    public class ChannelRead
    {
        private string pageString = "";
        Common.Net.Request request = null;
        List<string> albumPages = null;
        public ChannelRead()
        {
            albumPages = new List<string>();
        }

        public List<RohonModels.Models.Album> Read(string ChannelURL, bool next)
        {
            System.Net.CookieContainer container = new System.Net.CookieContainer();
            request = new Common.Net.Request(container);
            List<RohonModels.Models.Album> albums = new List<RohonModels.Models.Album>();
            pageString = request.ReadUrlContent(ChannelURL, MMVisitor.encode);
            int startIndex = pageString.IndexOf("<div class=\"imgList\">");
            if (startIndex <= 0)
            {
                return albums;
            }
            string imgList1 = pageString.Substring(pageString.IndexOf("<div class=\"imgList\">"));
            imgList1 = imgList1.Substring(0, imgList1.IndexOf("</div>"));
            string[] imgs1 = imgList1.Split(new string[] { "</li><li" }, System.StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < imgs1.Length; i++)
            {
                RohonModels.Models.Album album = new RohonModels.Models.Album();
                string s = imgs1[i].Substring(imgs1[i].IndexOf("href='") + 6);
                s = s.Substring(0, s.IndexOf("'"));
                string _url = ChannelURL;
                _url = _url.Substring(0, _url.LastIndexOf("/") - 1);
                _url = _url.Substring(0, _url.LastIndexOf("/"));
                album.Url = _url + s;
                s = imgs1[i].Substring(imgs1[i].IndexOf("alt='") + 5);
                s = s.Substring(0, s.IndexOf("'"));
                album.Name = s;
                albums.Add(album);
            }
            return albums;
        }

        public void GetPages(string str, string url)
        {
            string pages = str.Substring(str.IndexOf("<div class=\"page both\">") + 23);
            pages = pages.Substring(0, pages.IndexOf("</div>"));



            string[] pagearray = pages.Split(new string[] { "</li>\r\n<li" }, System.StringSplitOptions.RemoveEmptyEntries);
            for (int i = 1; i < pagearray.Length - 1; i++)
            {
                if (pagearray[i].IndexOf("#") < 0)
                {
                    string pager = pagearray[i].Substring(pagearray[i].IndexOf("href='") + 6);
                    pager = pager.Substring(0, pager.IndexOf("'"));
                    pager = url.Substring(0, url.LastIndexOf("/") + 1) + pager;
                    bool isInAlbum = false;
                    foreach (string s in albumPages)
                    {
                        if (s.Equals(pager))
                        {
                            isInAlbum = true;
                        }
                    }
                    if (!isInAlbum)
                    {
                        albumPages.Add(pager);
                        pager.Debug();
                    }
                }
                //pager.Debug();
            }



        }

    }
}
